package com.android.demo;

import android.app.Activity;
import android.app.ActivityGroup;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Process;
import android.provider.CallLog.Calls;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.ImageView;
import android.widget.TabHost;
import android.widget.TextView;
import android.widget.TabHost.TabSpec;

public class TestActivityGroup extends ActivityGroup {
//	private static final String TAG = "TestActivityGroup";
	private TabHost mTabHost; 
	
//	private static String TAB_CONTACTS = "contacts";
//	private static String TAB_CALL_LOG = "call_log";
//	private static String TAB_DIALER = "dialer";
//	private static String TAB_DYNAMIC = "dynamic";
//	private static String TAB_IM = "im";

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.main_activity);
		
		initActivity();
	}

	public void hideTab(boolean bHide) {
		if (bHide) {
			mTabHost.getTabWidget().setVisibility(View.GONE);
		} else {
			mTabHost.getTabWidget().setVisibility(View.VISIBLE);
		}
	}
	
	
	private void addTab(String tag, Class activity,int StringID,int drawalbeID,int backgroundID){
		// add contact list
		TabSpec mTab = mTabHost.newTabSpec(tag);
		
		mTab.setIndicator(
		inflaterTab( getResources().getDrawable(drawalbeID),
				getResources().getString(StringID),
				getResources().getDrawable(backgroundID)));
		
		mTab.setContent(new Intent(this, activity));
		mTabHost.addTab(mTab);
	}
	
	private void initActivity() {
		this.setVisible(true);

		mTabHost = (TabHost) findViewById(R.id.tabhost);
		mTabHost.setup(this.getLocalActivityManager());

		addTab("tag" ,Test.class, R.string.app_name, R.drawable.vk_zoom_handle, R.drawable.vk_button_selected);
		addTab("tag" ,Test.class, R.string.app_name, R.drawable.vk_zoom_handle, R.drawable.vk_button_selected);
		addTab("tag" ,Test.class, R.string.app_name, R.drawable.vk_zoom_handle, R.drawable.vk_button_selected);
		
		// add contact list
//		TabSpec mTab = mTabHost.newTabSpec(TAB_CONTACTS);
//		mTab.setIndicator(inflaterTab(getResources().getDrawable(R.drawable.ic_contact),
//				getResources().getString(R.string.tab_txt_contacts)));
//		mTab.setContent(new Intent(this, ContactsListActivity.class));
//		mTabHost.addTab(mTab);
//
//		// add call log
//		mTab = mTabHost.newTabSpec(TAB_CALL_LOG);
//		mTab.setIndicator(inflaterTab(getResources().getDrawable(R.drawable.ic_calllog),
//				getResources().getString(R.string.tab_txt_call_log)));
//		mTab.setContent(new Intent(this, CallHistoryActivity.class));		
//		mTabHost.addTab(mTab);
//
//		// add dialer
//		mTab = mTabHost.newTabSpec(TAB_DIALER);
//		mTab.setIndicator(inflaterTab(getResources().getDrawable(R.drawable.ic_dialer),
//				getResources().getString(R.string.tab_txt_dialer)));
//		mTab.setContent(new Intent(this, DialerActivity.class));				
//		mTabHost.addTab(mTab);
//
//		// add dynamic
//		mTab = mTabHost.newTabSpec(TAB_DYNAMIC);
//		mTab.setIndicator(inflaterTab(getResources().getDrawable(R.drawable.ic_dynamic),
//				getResources().getString(R.string.tab_txt_dynamic)));
//		mTab.setContent(new Intent(this, DynamicActivity.class));				
//		mTabHost.addTab(mTab);
//
//		// add IM
//		mTab = mTabHost.newTabSpec(TAB_IM);
//		mTab.setIndicator(inflaterTab(getResources().getDrawable(R.drawable.ic_im),
//				getResources().getString(R.string.tab_txt_im)));
//		mTab.setContent(new Intent(this, ConversationListActivity.class));				
//		mTabHost.addTab(mTab);
		
		// find first show up tab
//		final Intent intent = getIntent();
//		fixIntent(intent);
//		if (intent.getAction().equals(Intent.ACTION_CALL_BUTTON)) {
//			mTabHost.setCurrentTabByTag(TAB_DIALER);
//		} else if (intent.getAction().equals(Intent.ACTION_VIEW)) {
//			mTabHost.setCurrentTabByTag(TAB_CALL_LOG);
//		} else {
//			mTabHost.setCurrentTabByTag(TAB_CONTACTS);
//		}
	}

	private View inflaterTab(Drawable icon, String strText,Drawable backgroudDrable){
    	View view = LayoutInflater.from(this).inflate(R.layout.tab_item_layout, null);
    	ViewGroup layout = (ViewGroup) view.findViewById(R.id.tab_bar_layout);
    	layout.setBackgroundDrawable(backgroudDrable);
    	ImageView imageView = (ImageView) view.findViewById(R.id.tab_imageView);
    	imageView.setImageDrawable(icon); 
    	TextView textView =  (TextView) view.findViewById(R.id.tab_textView);
    	textView.setText(strText);
    	
    	return view;
	}

//	private void fixIntent(Intent intent) {
//		if (Intent.ACTION_CALL_BUTTON.equals(intent.getAction())) {
//			intent.setDataAndType(Calls.CONTENT_URI, Calls.CONTENT_TYPE);
//			intent.putExtra("call_key", true);
//			setIntent(intent);
//		}
//		if (Intent.ACTION_VIEW.equals(intent.getAction())) {
//			intent.setDataAndType(Calls.CONTENT_URI, Calls.CONTENT_TYPE);
//			setIntent(intent);
//		}
//	}

//	@Override
//	public boolean dispatchTouchEvent(MotionEvent ev) {
//
//		if (mTabHost.getTabWidget().getVisibility() == View.VISIBLE){					
//			// contact tab and dialer tab need a click event
//			if ((mTabHost.getCurrentTab() == 0 || mTabHost.getCurrentTab() == 2) && ev.getAction() == MotionEvent.ACTION_UP){
//				Rect r = new Rect(); 
//				int vLoc[] = new int[2];
//				mTabHost.getTabWidget().getChildTabViewAt(mTabHost.getCurrentTab()).getHitRect(r);
//				mTabHost.getTabWidget().getChildTabViewAt(mTabHost.getCurrentTab()).getLocationOnScreen(vLoc);
//				
//				if (r.contains((int)ev.getX(), (int)ev.getY() - vLoc[1])){
//					Activity activity = null;
//					if (mTabHost.getCurrentTab() == 0){
//						activity = getLocalActivityManager().getActivity(TAB_CONTACTS);
//					
//						// when click tab, send message to contacts activity to toggle
//						if (activity != null){
//							((ContactsListActivity)activity).doToggle();
//						}
//					}else if (mTabHost.getCurrentTab() == 2){
//						activity = getLocalActivityManager().getActivity(TAB_DIALER);
//						
//						// when click tab, send message to contacts activity to toggle
//						if (activity != null){
//							((DialerActivity)activity).doToggle();
//						}
//					}
//					
//				}
//			}
//		}
//
//		super.dispatchTouchEvent(ev);
//		return false;
//	}

	@Override
	public boolean dispatchKeyEvent(KeyEvent event) {
		if (event.getKeyCode() == KeyEvent.KEYCODE_SEARCH) {
			//Intent i = new Intent(this, SearchContactActivity.class);
			//startActivity(i);
			return true;
		} else {
			return super.dispatchKeyEvent(event);
		}
	}

	public boolean onCreateOptionsMenu(Menu menu) {
		//		menu.add(0, 1, 1, getResources().getString(R.string.main_activity_menu_option)).setIcon(android.R.drawable.ic_menu_preferences);
		//		menu.add(0, 2, 2, getResources().getString(R.string.main_activity_menu_exit)).setIcon(android.R.drawable.ic_menu_close_clear_cancel);
		return true;
	}

	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case 1:
			// start option activity
//			Intent i = new Intent(this, OptionActivity.class);
//			startActivity(i);
			break;

		case 2:
			Process.killProcess(Process.myPid());
			break;

		}

		return super.onOptionsItemSelected(item);
	}

}
